package com.opentd.npc;

import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Point;

import javax.swing.ImageIcon;

import com.opentd.game.Game;
import com.opentd.graphics.Drawable;

public class Enemy {
	public int id, x, y;
	public int health;
	public Image sprite, heart;
	public boolean inGame = false;
	public static final int RIGHT = 0, LEFT = 1, UP = 2, DOWN = 3;
	public int wayWeCameFrom = LEFT;
	public int direction = RIGHT;
	public MoveTask moveTask;
	public int value;
	
	public int pixelX, pixelY;
	public Point center;
	
	public Enemy(int gridX, int gridY, int id, int health){
		this.x = gridX;
		this.y = gridY;
		this.id = id;
		this.health = health;
		if(id == 1){//Normal enemy
			sprite = new ImageIcon("dat/img/enemy_normal.png").getImage();
		} else if(id == 2){//Boss
			sprite = new ImageIcon("dat/img/enemy_boss.png").getImage();
		}
		heart = new ImageIcon("dat/img/heart.png").getImage();
		value = 10;
	}

	public void drawMe(Graphics g) {
		if(inGame){
			g.drawImage(sprite, (pixelX), (pixelY), Game.Block.WIDTH, Game.Block.HEIGHT, null);
			//g.drawString("Direction = " + (direction == RIGHT ? "Right" : direction == LEFT ? "Left" : direction == UP ? "Up" : "Down"), me.pixelX, me.pixelY);
			//g.drawString("Way We Came From = " + (wayWeCameFrom == RIGHT ? "Right" : wayWeCameFrom == LEFT ? "Left" : wayWeCameFrom == UP ? "Up" : "Down"), pixelX, pixelY + 30);
			//g.drawString("(" + me.x + ", " + me.y + ")", pixelX, pixelY + 60);
		}
	}
	
	public void drawHealth(Graphics g){
		if(inGame){
			g.setFont(new Font(Font.DIALOG, Font.PLAIN, 12));
			//Draw the health and the heart next to it
			g.drawImage(heart, (pixelX + 10), (pixelY - 15), 15, 15, null);
			g.drawString(" " + health, pixelX + 25, pixelY);
		}
	}

	public void logic() {
		if(inGame){
			if(isAtFinishTile()){
				reachedEnd();
				return;
			}
			if(health < 1){
				die();
			}
			if(!canContinue()){
				if(!Game.selectedLevel.map.getBlock(x + 1, y).clipped && wayWeCameFrom != RIGHT){
					wayWeCameFrom = LEFT;
					direction = RIGHT;
				}else if(!Game.selectedLevel.map.getBlock(x, y - 1).clipped && wayWeCameFrom != UP){
					wayWeCameFrom = DOWN;
					direction = UP;
				}else if(!Game.selectedLevel.map.getBlock(x , y + 1).clipped && wayWeCameFrom != DOWN){
					wayWeCameFrom = UP;
					direction = DOWN;
				}else if(!Game.selectedLevel.map.getBlock(x - 1, y).clipped && wayWeCameFrom != LEFT){
					wayWeCameFrom = RIGHT;
					direction = LEFT;
				}
			}
			moveTask.step(this);
		}
	}
	
	public boolean canContinue(){
		try{
			if(direction == RIGHT){
				if(Game.selectedLevel.map.getBlock(x + 1, y).clipped){
					return false;
				}
			}
			if(direction == LEFT){
				if(Game.selectedLevel.map.getBlock(x - 1, y).clipped){
					return false;
				}
			}
			if(direction == UP){
				if(Game.selectedLevel.map.getBlock(x, y - 1).clipped){
					return false;
				}
			}
			if(direction == DOWN){
				if(Game.selectedLevel.map.getBlock(x, y + 1).clipped){
					return false;
				}
			}
		}catch(NullPointerException e){
			return false;
		}
		return true;
	}

	public void addToGame() {
		//Set the enemy to be on the spawn block
		x = Game.selectedLevel.map.spawnBlock.x;
		y = Game.selectedLevel.map.spawnBlock.y;
		pixelX = x * Game.Block.WIDTH;
		pixelY = y * Game.Block.HEIGHT;
		
		center = new Point(pixelX + (Game.Block.WIDTH /2), pixelY + (Game.Block.HEIGHT /2));
		moveTask = new MoveTask();
		inGame = true;
	}
	
	public boolean isAtFinishTile(){
		if(x == Game.selectedLevel.map.finishBlock.x && y == Game.selectedLevel.map.finishBlock.y)
			return true;
		return false;
	}
	
	public void reachedEnd(){
		Game.selectedLevel.player.damage(health);
		inGame = false;
	}
	
	public void die(){
		Game.selectedLevel.money += value;
		inGame = false;
	}
	
	public void damage(int amount){
		health -= amount;
	}
	
	public void restore(int amount){
		health += amount;
	}
}